package run.halo.app.content;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;
import org.springframework.util.MultiValueMap;

@Data
@Accessors(chain = true)
public class IMXWGGListPostRequestDto {
    @Schema(requiredMode = REQUIRED,name = "time",implementation = String.class,description = "时间戳,System.currentTimeMillis()/1000")
    private String time;
    @Schema(requiredMode = REQUIRED,name = "nonce",implementation = String.class,description = "随机数")
    private String nonce;
    @Schema(requiredMode = REQUIRED,name = "userId",implementation = String.class,description = "当前用户ID，对应云办公账号ID")
    private String userId;
    @Schema(requiredMode = REQUIRED,name = "wxUserId",implementation = String.class,description = "当前用户企业微信ID")
    private String wxUserId;
    @Schema(requiredMode = REQUIRED,name = "maxCount",implementation = Integer.class,description = "查询的最大数量；组件类型为：广告轮播、消息列表、汇总、待办 时，该字段存在",example = "3")
    private Integer maxCount;
    @Schema(requiredMode = REQUIRED,name = "tabMaxCount",implementation = Integer.class,description = "分组的最大数量；组件类型为：消息列表 时，该字段存在；",example = "3")
    private Integer tabMaxCount;
    @Schema(requiredMode = REQUIRED,name = "currentPage",implementation = Integer.class,description = "当前页",example = "1")
    private Integer currentPage;
    @Schema(requiredMode = NOT_REQUIRED,name = "typeId",implementation = String.class,description = "分类ID；组件类型为：消息列表 时，该字段存在；",example = "category-CdsmZ")
    private String typeId;
    @Schema(requiredMode = REQUIRED,name = "allTypeId",implementation = String.class,description = "该数据源需要查询的所有分类",example = "category-CdsmZ,category-TdMWU")
    // @ArraySchema
    private List<String> allTypeId;

    /**
     * Creates from snapshot.
     */
    @SneakyThrows
    public static IMXWGGListPostRequestDto from(MultiValueMap<String, String> formData) {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> formDataView = formData.asSingleValueMap();
        HashMap<String, Object> tmp = new HashMap<>(formDataView);
        if (formDataView.containsKey("allTypeId")) {
            List<String> list = Stream.of(formDataView.get("allTypeId").split(","))
                .toList();
            tmp.put("allTypeId",list);
        }
        String asString = mapper.writeValueAsString(tmp);
        return mapper.readValue(asString, IMXWGGListPostRequestDto.class);
    }
}
